package com.ubxtech.db.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ubxtech.core.context.ContextToken;
import com.ubxtech.core.context.ContextUtil;
import com.ubxtech.db.properties.DbProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;

import java.util.Set;

/**
 * @author Victor.Xiao
 * @since 2023-10-10 17:53
 **/
@Slf4j
public class TenantMultiValueHandlerImpl extends TenantEqValueHandlerImpl implements TenantMultiValueHandler {

    private DbProperties dbProperties;


    public TenantMultiValueHandlerImpl(DbProperties dbProperties){
        super(dbProperties);
        this.dbProperties = dbProperties;
    }

    @Override
    public ItemsList getTenantIds() {
        ContextToken contextToken = ContextUtil.getContextToken();
        if (contextToken == null) {
            return null;
        }
        Set<String> tenants = contextToken.getTenantMulti();
        if (CollectionUtil.isEmpty(tenants)) {
            return null;
        }
        ExpressionList itemsList = new ExpressionList();
        for (String tenant : tenants) {
            itemsList.addExpressions(new StringValue(tenant));
        }
        return itemsList;
    }


    /**
     * 租户字段查询方式,eq:等于,in:多个,like:匹配,默认是eq
     * @author Victor.Xiao
     * @since 2023-10-10 19:55
     * @return java.lang.String
     */
    @Override
    public String getSelectType() {
        return dbProperties.getTenantSelectType();
    }

}
